import 'package:applications/module/home/bean/application_bean.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'application_po.freezed.dart';
part 'application_po.g.dart';

@unfreezed
class ApplicationPo with _$ApplicationPo {
  // const ApplicationPo._();

  factory ApplicationPo({
    required final int id,

    ///
    /// 标题
    ///
    required String name,

    ///
    /// 图标
    ///
    required final String image,

    ///
    /// 类别
    ///
    required final String category,

    ///
    /// 评价等级
    ///
    required double averageUserRating,

    ///
    /// 评价数
    ///
    required int userRatingCount,

    ///
    /// 应用描述
    ///
    required final String? description,

    ///
    /// 作者名称
    ///
    required final String? artistName,

    ///
    /// 排序属性
    /// 批量存入数据库时是无序的,该属性用于保存api返回数据顺序
    ///
    required int sort,

    ///
    /// 列表是否首次加载
    /// 用于控制列表加载时是否请求详情api获取评价等级信息
    ///
    @Default(true) bool isFrist,
  }) = _ApplicationPo;

  factory ApplicationPo.fromJson(Map<String, Object?> json) =>
      _$ApplicationPoFromJson(json);

  factory ApplicationPo.beanToPo(Entry entry, int sort) => ApplicationPo(
        id: int.tryParse(entry.id?.attributes?.imId ?? '0') ?? 0,
        name: entry.title?.label ?? '',
        image: entry.imImage?.last.label ?? '',
        category: entry.category?.attributes?.label ?? '',
        averageUserRating: 0,
        userRatingCount: 0,
        description: '',
        artistName: '',
        sort: sort,
      );
}
